<script>
import { cartStore } from "../store"
import { mapActions } from "pinia";
export default {
    props: {
        info: Object
    },
    data() {
        return {
            colors: {
                '白色': '#ffffff',
                '金色': '#dac272',
                '蓝色': '#233472',
                '红色': '#f2352e'
            }
        }
    },
    mounted() {
    },
    methods: {
        // 不使用 setup()
        ...mapActions(cartStore, ['addCart']),
        handleCart() {
            // cart.addCart(this.info.id)
            console.log("添加购物车成功", this.info.id);
            this.addCart(this.info.id);
        }
    }
};
</script>

<template>
    <div class="product-card">
        <router-link :to="'/product/' + info.id" class="product-main">
            <img :src="info.image">
            <h3 class="product-name">{{ info.name }}</h3>
            <div :style="{ background: colors[info.color] }"></div>
            <div class="product-cost">¥ {{ info.cost }}</div>
            <div class="product-add-cart" @click.prevent="handleCart">加入购物车</div>
        </router-link>
    </div>
</template>

<style scoped>
.product-card {
    width: 100%;
    break-inside: avoid;
    border: 1px solid #dddee1;
    border-radius: 5px;
    margin-top: 20px;
    box-shadow: 1px 1px 5px 1px rgba(0, 0, 0, 0.062);
}

.product-card a {
    text-decoration: none;
    color: black;

}

.product-card img {
    width: 100%;
    height: 100%;
}

.product-cost {
    color: red;
}


.product-main {
    display: block;
    margin: 16px;
    padding: 16px;
    border-radius: 6px;
    overflow: hidden;
    background: #fff;
    text-align: center;
    position: relative;
}

.product-main img {
    width: 100%;
}

h4 {
    color: #222;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.product-main:hover h4 {
    color: #0070c9;
}

.product-color {
    display: block;
    width: 16px;
    height: 16px;
    border: 1px solid #dddee1;
    border-radius: 50%;
    margin: 6px auto;
}

.product-cost {
    color: #de4037;
    margin-top: 6px;
}

.product-add-cart {
    display: none;
    padding: 4px 8px;
    background: #2d8cf0;
    color: #fff;
    font-size: 12px;
    border-radius: 3px;
    cursor: pointer;
    position: absolute;
    top: 5px;
    right: 5px;
}

.product-main:hover .product-add-cart {
    display: inline-block;
}
</style>
